﻿Imports System.Data
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Web.Http
Imports System.Web.Http.Description
Imports BIWEBAPI
Imports System.Web.Http.Cors

Namespace Controllers
    Public Class ApiFacebookSharedClicksSetsController
        Inherits System.Web.Http.ApiController

        Private db As New BI_GUIDELINESConnection

        ' GET: api/ApiFacebookSharedClicksSets
        ''' <summary>
        ''' Returns A IQueryable(Of FacebookSharedClicksSet) Records
        ''' </summary>
        ''' <returns>A IQueryable(Of FacebookSharedClicksSet)</returns>
        ''' <remarks></remarks>
        <AllowAnonymous>
        <EnableCors("*", "*", "*")>
      <ResponseType(GetType(Void))>
        Function GetFacebookSharedClicksSets() As IQueryable(Of FacebookSharedClicksSet)
            Return db.FacebookSharedClicksSets
        End Function


        ' GET: api/ApiFacebookSharedClicksSets/5
        ''' <summary>
        ''' Returns FacebookSharedClick record using an id
        ''' </summary>
        ''' <param name="id"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <AllowAnonymous>
        <EnableCors("*", "*", "*")>
        <ResponseType(GetType(FacebookSharedClicksSet))>
        Function GetFacebookSharedClicksSet(ByVal id As Integer) As IHttpActionResult
            Dim facebookSharedClicksSet As FacebookSharedClicksSet = db.FacebookSharedClicksSets.Find(id)
            If IsNothing(facebookSharedClicksSet) Then
                Return NotFound()
            End If

            Return Ok(facebookSharedClicksSet)
        End Function

        ' PUT: api/ApiFacebookSharedClicksSets/5
        ''' <summary>
        ''' Updates an especific record using it's id
        ''' </summary>
        ''' <param name="id"></param>
        ''' <param name="facebookSharedClicksSet"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <AllowAnonymous>
      <EnableCors("*", "*", "*")>
      <ResponseType(GetType(Void))>
        Function PutFacebookSharedClicksSet(ByVal id As Integer, ByVal facebookSharedClicksSet As FacebookSharedClicksSet) As IHttpActionResult
            If Not ModelState.IsValid Then
                Return BadRequest(ModelState)
            End If

            If Not id = facebookSharedClicksSet.FacebookSharedClicksId Then
                Return BadRequest()
            End If

            db.Entry(facebookSharedClicksSet).State = EntityState.Modified

            Try
                db.SaveChanges()
            Catch ex As DbUpdateConcurrencyException
                If Not (FacebookSharedClicksSetExists(id)) Then
                    Return NotFound()
                Else
                    Throw
                End If
            End Try

            Return StatusCode(HttpStatusCode.NoContent)
        End Function

        ' POST: api/ApiFacebookSharedClicksSets
        ''' <summary>
        ''' Saves a new ApiFacebookSharedClicks in the data base
        ''' </summary>
        ''' <param name="facebookSharedClicksSet"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <AllowAnonymous>
      <EnableCors("*", "*", "*")>
      <ResponseType(GetType(FacebookSharedClicksSet))>
        Function PostFacebookSharedClicksSet(ByVal facebookSharedClicksSet As FacebookSharedClicksSet) As IHttpActionResult
            If Not ModelState.IsValid Then
                Return BadRequest(ModelState)
            End If

            db.FacebookSharedClicksSets.Add(facebookSharedClicksSet)
            db.SaveChanges()

            Return CreatedAtRoute("DefaultApi", New With {.id = facebookSharedClicksSet.FacebookSharedClicksId}, facebookSharedClicksSet)
        End Function

        ' DELETE: api/ApiFacebookSharedClicksSets/5
        ''' <summary>
        '''Deletes a record by id
        ''' </summary>
        ''' <param name="id"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <ResponseType(GetType(FacebookSharedClicksSet))>
        Function DeleteFacebookSharedClicksSet(ByVal id As Integer) As IHttpActionResult
            Dim facebookSharedClicksSet As FacebookSharedClicksSet = db.FacebookSharedClicksSets.Find(id)
            If IsNothing(facebookSharedClicksSet) Then
                Return NotFound()
            End If

            db.FacebookSharedClicksSets.Remove(facebookSharedClicksSet)
            db.SaveChanges()

            Return Ok(facebookSharedClicksSet)
        End Function
        
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing) Then
                db.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub
       
        Private Function FacebookSharedClicksSetExists(ByVal id As Integer) As Boolean
            Return db.FacebookSharedClicksSets.Count(Function(e) e.FacebookSharedClicksId = id) > 0
        End Function
    End Class
End Namespace